package com.zjuee.web.tporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.utils.DesensitizationUtil;
import com.zjuee.service.TransAffairsReplyService;
import com.zjuee.base.dto.common.tporder.TransAffairsReplyDto;
import com.zjuee.base.model.common.tporder.WkTransAffairsCompanyEntity;
import com.zjuee.base.model.common.tporder.WkTransAffairsFileEntity;
import com.zjuee.base.model.common.tporder.WkTransAffairsMaterialEntity;
import com.zjuee.base.model.common.tporder.WkTransAffairsReplyEntity;
import com.zjuee.vo.WkTransAffairsCompanyVo;
import com.zjuee.vo.WkTransAffairsMaterialVo;
import com.zjuee.vo.WkTransAffairsReplyVo;
import com.zjuee.web.tporder.mapper.TransAffairsCompanyMapper;
import com.zjuee.web.tporder.mapper.TransAffairsFileMapper;
import com.zjuee.web.tporder.mapper.TransAffairsMaterialMapper;
import com.zjuee.web.tporder.mapper.TransAffairsReplyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TransAffairsReplyImpl implements TransAffairsReplyService {
    @Resource
    private TransAffairsReplyMapper transAffairsReplyMapper;
    @Resource
    private TransAffairsCompanyMapper transAffairsCompanyMapper;
    @Resource
    private TransAffairsMaterialMapper transAffairsMaterialMapper;
    @Resource
    private TransAffairsFileMapper transAffairsFileMapper;
//    @Resource
//    private CompanyLicenseClient companyLicenseClient;
    @Resource
    private LoginDto loginDto;

    @Override
    public Long add(WkTransAffairsReplyVo type) {
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(WkTransAffairsReplyVo type) {
        return false;
    }

    @Override
    public WkTransAffairsReplyVo selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<WkTransAffairsReplyVo> list(TransAffairsReplyDto dto) {
//        Long orgBusinessId = loginDto.getOrgBusinessId();
//        List<CompanyLicenseVo> list = companyLicenseClient.selectByCompanyId(orgBusinessId).getData();
//        if (CollectionUtils.isEmpty(list)){
//            return dto.getPage().convert();
//        }
//        List<String> bclCodeList = list.stream().map(CompanyLicenseVo::getBclCode).collect(Collectors.toList());
//        dto.setBclCodes(bclCodeList);

        Page<WkTransAffairsReplyVo> page = transAffairsReplyMapper.getTransAffairsReplyList(dto.getPage().convert(), dto);
        page.getRecords().forEach(p -> {
            p.setWtarSqrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtarSqrq()));
            if (p.getWtarHfrq() != null) {
                p.setWtarHfrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtarHfrq()));
            }
            p.setWtarKsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtarKsrq()));
            p.setWtarJsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getWtarJsrq()));
            p.setWtarWfycdwlxr(DesensitizationUtil.desensitizedName(p.getWtarWfycdwlxr()));
            p.setWtarFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(p.getWtarFwycdwlxrsj()));
            p.setWtarWfjsdwlxr(DesensitizationUtil.desensitizedName(p.getWtarWfjsdwlxr()));
            p.setWtarWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(p.getWtarWfjsdwlxrsj()));
        });
        return page;
    }

    @Override
    public LambdaQueryWrapper<WkTransAffairsReplyVo> fuzzyQuery(TransAffairsReplyDto dto) {
        return null;
    }

    @Override
    public WkTransAffairsReplyVo getTransAffairsReply(Long id) {
        WkTransAffairsReplyEntity wkTransAffairsReplyEntity = transAffairsReplyMapper.selectById(id);

        QueryWrapper<WkTransAffairsCompanyEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wtaa_id", id);
        List<WkTransAffairsCompanyEntity> wkTransAffairsCompanyEntitys = transAffairsCompanyMapper.selectList(queryWrapper1);

        QueryWrapper<WkTransAffairsMaterialEntity> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("wtaa_id", id);
        List<WkTransAffairsMaterialEntity> wkTransAffairsMaterialEntitys = transAffairsMaterialMapper.selectList(queryWrapper2);

        QueryWrapper<WkTransAffairsFileEntity> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("wta_id", id);
        List<WkTransAffairsFileEntity> wkTransAffairsFileEntities = transAffairsFileMapper.selectList(queryWrapper3);
        WkTransAffairsReplyVo wkTransAffairsReplyVo =  new WkTransAffairsReplyVo();

        List<WkTransAffairsCompanyVo> wkTransAffairsCompanyVos = new ArrayList<>();

        List<WkTransAffairsMaterialVo> wkTransAffairsMaterialVos = new ArrayList<>();

        BeanUtils.copyProperties(wkTransAffairsReplyEntity, wkTransAffairsReplyVo);
        for(WkTransAffairsCompanyEntity wkTransAffairsCompanyEntity : wkTransAffairsCompanyEntitys){
            WkTransAffairsCompanyVo wkTransAffairsCompanyVo = new WkTransAffairsCompanyVo();
            BeanUtils.copyProperties(wkTransAffairsCompanyEntity, wkTransAffairsCompanyVo);
            wkTransAffairsCompanyVo.setWtacYsdwlxr(DesensitizationUtil.desensitizedName(wkTransAffairsCompanyVo.getWtacYsdwlxr()));
            wkTransAffairsCompanyVo.setWtacYsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransAffairsCompanyVo.getWtacYsdwlxrsj()));
            wkTransAffairsCompanyVos.add(wkTransAffairsCompanyVo);
        }
        for(WkTransAffairsMaterialEntity wkTransAffairsMaterialEntity : wkTransAffairsMaterialEntitys){
            WkTransAffairsMaterialVo wkTransAffairsMaterialVo = new WkTransAffairsMaterialVo();
            BeanUtils.copyProperties(wkTransAffairsMaterialEntity, wkTransAffairsMaterialVo);
            wkTransAffairsMaterialVos.add(wkTransAffairsMaterialVo);
        }

        wkTransAffairsReplyVo.setWtarSqrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsReplyVo.getWtarSqrq()));
        if (wkTransAffairsReplyVo.getWtarHfrq() != null) {
            wkTransAffairsReplyVo.setWtarHfrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsReplyVo.getWtarHfrq()));
        }
        wkTransAffairsReplyVo.setWtarKsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsReplyVo.getWtarKsrq()));
        wkTransAffairsReplyVo.setWtarJsrqStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(wkTransAffairsReplyVo.getWtarJsrq()));
        wkTransAffairsReplyVo.setWkTransAffairsCompanyVos(wkTransAffairsCompanyVos);
        wkTransAffairsReplyVo.setWkTransAffairsMaterialVos(wkTransAffairsMaterialVos);
        wkTransAffairsReplyVo.setWkTransAffairsFileEntities(wkTransAffairsFileEntities);
        //脱敏处理
        wkTransAffairsReplyVo.setWtarWfycdwlxr(DesensitizationUtil.desensitizedName(wkTransAffairsReplyVo.getWtarWfycdwlxr()));
        wkTransAffairsReplyVo.setWtarFwycdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransAffairsReplyVo.getWtarFwycdwlxrsj()));
        wkTransAffairsReplyVo.setWtarWfjsdwlxr(DesensitizationUtil.desensitizedName(wkTransAffairsReplyVo.getWtarWfjsdwlxr()));
        wkTransAffairsReplyVo.setWtarWfjsdwlxrsj(DesensitizationUtil.desensitizedPhoneNumber(wkTransAffairsReplyVo.getWtarWfjsdwlxrsj()));
        return wkTransAffairsReplyVo;
    }
}
